# -*- coding: utf-8 -*-

'''
Created on 27/02/2010

@author: vincent.vanghelle
'''

from django import forms
from django.contrib.formtools.wizard import FormWizard
from django.http import HttpResponseRedirect
from mmorganizer.games.models import Game, Class, Role, Serveur
from mmorganizer.characters.models import Personnage, Roster
        
class CharacterCreationForm(forms.ModelForm):
    game = forms.CharField(max_length=50,widget=forms.widgets.HiddenInput())
    user = forms.CharField(max_length=50,widget=forms.widgets.HiddenInput(),required=False)
    serveur = forms.ModelChoiceField(queryset=Serveur.objects.all())
    classe = forms.ModelChoiceField(queryset=Class.objects.all(),widget=forms.Select(attrs={'onchange':'get_roles_associes();'}))
    role = forms.ModelChoiceField(queryset=Role.objects.all())
    name = forms.CharField(max_length=50)
    
    def specifierIdJeu(self,game_id):
        self.fields['game'].initial=game_id
        self.fields['serveur'].queryset = Serveur.objects.filter(game__id=game_id)
        self.fields['classe'].queryset = Class.objects.filter(game__id=game_id)
        
    class Meta:
        model = Personnage
        
    def saveData(self, commit=True, user=None):
        game = Game.objects.get(id=self.cleaned_data['game'])
        self.cleaned_data['game']=game
        self.cleaned_data['user']=user
        perso = self.save(commit=True)
        profile = user.get_profile()
        if profile.defaultPersonnage == None:
            profile.defaultPersonnage = perso
            profile.save()
        return perso
    
class RosterCreationForm(forms.ModelForm):
    game = forms.CharField(max_length=50,widget=forms.widgets.HiddenInput())
    responsable = forms.CharField(max_length=50,widget=forms.widgets.HiddenInput(),required=False)
    serveur = forms.ModelChoiceField(queryset=Serveur.objects.all())
    name = forms.CharField(max_length=50)
    
    def specifierIdJeu(self,game_id):
        self.fields['game'].initial=game_id
        self.fields['serveur'].queryset = Serveur.objects.filter(game__id=game_id)
        
    class Meta:
        model = Roster
        exclude=['membres','membresEnAttente','admins']
        
    def saveData(self, commit=True, responsable=None):
        game = Game.objects.get(id=self.cleaned_data['game'])
        self.cleaned_data['game']=game
        self.cleaned_data['responsable']=responsable
        roster = self.save(commit=True)
        #roster.admins.add(responsable)
        return roster